<?php 

namespace Admin\Logic;

class IndexLogic{
	//获得活动
	public function getActions($status,$firstRow,$listRows){
		$a = D('Action');
	  	return $a->where('status='.$status)->order('start_time desc')->limit($firstRow.','.$listRows)->select();
	}

	public function countAction($status){
		$s = D("Action");
		return $s->where('status='.$status)->count();
	}

	//一个活动
	public function getAction($id){
		$a = D('Action');
	  	return $a->where('id='.$id)->find();
	}

	//获得订单
	public function getOrders($status,$firstRow,$listRows){
		$o = D('Action_order');
		if($status==1){
			return $o->where(' status != 0 ')->order('create_time desc')->limit($firstRow.','.$listRows)->select();
		}

		return $o->where('status='.$status)->order('create_time desc')->limit($firstRow.','.$listRows)->select();
	}

	public function countOrder($status){
		$s = D("Action_order");
		if($status==1){
			return $s->where('status != 0 ' )->count();
		}
		return $s->where('status='.$status)->count();
	}

	public function getBarber($id){
		$b = D('Barber');
		return $b->where(array('id'=>$id))->find();
	}

	public function getType($id){
		$b = D('Marketing_model');
		return $b->where(array('id'=>$id))->find();
	}

	public function getStore($id){
		$st = D('Store');
		return $st->where('id='.$id)->find();
	}

	//查询订单号/手机号
	public function getSearch($data){
		$o = D('Action_order');
		return $o->where($data)->select();
	}

	//查询 门店名称
	public function getStoreName($name){
		$st = D('Store');
		$where['name'] = array('like','%'.$name.'%');
		$re = $st->where($where)->select();
		return $re;
	}

	//查询 获得订单
	public function searchOrders($where,$firstRow,$listRows){
		$o = D('Action_order');
		if($where['status']==1){
			unset($where['status']);
			$re = $o->where($where)->where(' status != 0 ')->order('create_time desc')->limit($firstRow.','.$listRows)->select();
		}else{
			$re =  $o->where($where)->order('create_time desc')->limit($firstRow.','.$listRows)->select();
		}
		
		return $re;
	}

	//查询 获得订单
	public function countSearchOrders($where){
		$o = D('Action_order');
		if($where['status']==1){
			unset($where['status']);
			$re = $o->where($where)->where(' status != 0 ')->count();
		}else{
			$re = $o->where($where)->count();
		}
		
		return $re;
	}

	//打印
	public function prints($mobile,$store_name,$status,$time1,$time2){
		$mysql_server_name="192.168.0.120"; //数据库服务器名称
	    $mysql_username="kaka"; // 连接数据库用户名
	    $mysql_password="kakameiye365"; // 连接数据库密码
	    $mysql_database="newmeiye"; // 数据库的名字
	    // 连接到数据库
	    $conn=mysql_connect($mysql_server_name, $mysql_username,
	                        $mysql_password)or die('网络错误，请稍后再试！');
	     mysql_query("set names 'utf8'");

	     mysql_select_db($mysql_database);

	    	$sql ="  SELECT o.order_number, o.mobile,o.store_id, o.price, o.create_time,o.status,o.barber_id,o.action_id
					FROM `action_order` o
					WHERE ";
			if($mobile){
				if(is_mobile($mobile)){
					$sql .="  o.mobile = {$mobile} ";
				}else{
					$sql .="  o.order_number = {$mobile} ";
				}	
			}
			if($store_name){
				if($mobile){
					$sql .= ' AND ';
				}
				$sql .="   o.store_id in ({$store_name}) ";
			}
			if($status>-1){
				if($mobile || $store_name){
					$sql .= ' AND ';
				}
				if($status==1){
					$sql .="   o.status != 0  ";
				}else{
					$sql .="   o.status = {$status}  ";
				}
				
			}

			$sql .= " AND create_time
					BETWEEN {$time1}
					AND {$time2}
					ORDER BY o.create_time DESC";		
            $re = mysql_query($sql);
            $str = "订单号,门店名称,手机号,发型师,参加活动,金额,下单时间,是否支付\n";
            while($row = mysql_fetch_assoc($re))
            {	
            	if($store_id != $row['store_id']){
            		unset($store_name);
	            	$store_sql= "SELECT name as store_name from store where `id`={$row['store_id']}";
	            	$store_re = mysql_query($store_sql);
	            	$store_name = mysql_fetch_assoc($store_re);
	            	$store_name = $store_name['store_name'];
	            }
	            $store_id = $row['store_id'];
            	if($row['barber_id']!=0){
            		if($barber_id != $row['barber_id']){
            					
	            		$barber_sql = "SELECT `name` as barber_name from barber where `id`={$row['barber_id']}";
	            		$barber_re = mysql_query($barber_sql);
		            	$barber_name = mysql_fetch_assoc($barber_re);
		            	$barber_name = $barber_name['barber_name'];
            		}
            		 $barber_id = $row['barber_id'];
	            }else{
	            	unset($barber_name);
	            }
            	
                $row['order_number']="'".$row['order_number']."'";
                $str .= $row["order_number"].",".$store_name.",".$row['mobile'].",".$barber_name.",";
                if($row['action_id']){
                	if($action_id != $$row['action_id']){
	                	unset($action_name);
	                	$action_sql = "SELECT `action_name`  from action where `id`={$row['action_id']}";
		            	$action_re = mysql_query($action_sql);
		            	$action_name = mysql_fetch_assoc($action_re);
		            	$action_name = $action_name['action_name'];	
	            	}
	            	$action_id = $row['action_id'];
                }else{
                	$action_name = '商城';
                }
                $str .= $action_name.",".($row['price']/100)."元,".date('Y-m-d H:i:s',$row['create_time']).",";
                if($row['status']==0){
                	$status = '否';
                }else{
                	$status = '是';
                }
                $str.= $status."\n";

                

            }

            header("Content-type:text/csv;charset=gbk");
            header("Content-Disposition:attachment;filename=fo.csv");
            header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
            header('Expires:0');
            header('Pragma:public');
            echo $str;   //读取文件到客户端

            mysql_close($conn); //关闭MySQL连接

	}


	
}


